package com.fjxd.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjxd.feign.clients.PeopleClient;
import com.fjxd.feign.pojo.People;
import com.fjxd.user.mapper.UserMapper;
import com.fjxd.user.pojo.User;
import com.fjxd.user.pojo.vo.UserVo;
import com.fjxd.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PeopleClient peopleClient;
    /*
    * 查询所有用户
    * */
    @Override
    public List<User> getAllUser() {
        return userMapper.selectList(null);
    }

    @Override
    public UserVo getUser(Integer id) {
        User user=userMapper.selectById(id);
        UserVo userVo=new UserVo();
        BeanUtils.copyProperties(user,userVo);
        List<People> people=peopleClient.getPeopleForUser(id);
        userVo.setPeople(people);
        return userVo;
    }

    @Override
    public boolean upPwd(Object id,Object newPwd,Object oldPwd) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,id);
        queryWrapper.eq(User::getPassword,oldPwd);
        User selectOne = userMapper.selectOne(queryWrapper);
        if (selectOne==null)
        {
            return false;
        }
        return userMapper.updateUser(id,newPwd);
    }
}
